<style>
	select{
		border:1px solid #ccc;
		height:34px;
		padding-left:10px;
		text-align:left;
		margin-right:20px;
	}
</style>
<?php include('header.php')?>
<html>
	<head>
		<link href="css/datatablecss.css" rel="stylesheet" type="text/css" />
        <script src="js/datatablejquery.js"></script>
        <script src="js/datatable.min.js"></script>
	</head>
    <body>
	<div class="">
        <form id="formid" method="post" action="">
		<div>
					<select id="app" name="app" onChange="return usersList()">
						<option value="" >select app</option>
						<option value="MAT">MobileTheft</option>
						<option value="FSU">FamilySafety</option>
						<option value="ER">Event Remainder</option>
						<option value="FRIENDS">Friends</option>
					</select>
					<select id="title" name="title">
						<option value="" >select news letter</option>
						
						<?php include('connect.php')?>
						<?
							$sql1=mysql_query("select * from newsletters");
							while($result=mysql_fetch_array($sql1)){?> 	   
							 <option value="<?php echo $result['news_id'];?> "><? echo $result['title'];?></option>
						<? }?>
					</select>
					<br/>
					<br/>
					<br/>
					<div id="tablediv">
						<div id="alertLoader" style="padding:5px 0px !important;text-align:center;"></div>
						<table id="example" class="display" cellspacing="0" width="100%">
							<thead>
								<tr>
									<th align="left" style="padding-left:10px;"><input type="checkbox" id="selectAll" onclick="checkAll();"></th>
									<th>Id</th>
									<th>email</th>
									<th>Added_date</th>
									<th>Type</th>
									<th>status</th>
									
								</tr>
							</thead>
							<tbody>
								<tr>
									<td class="product_table"></td>
								</tr>
							</tbody>
							
 
							
						</table>
					</div>
						<br/>
						<?php
						if(isset($_POST['checkbox'])){
								$resultSet='';
								foreach($_POST['checkbox'] as $checkboxvalue){
									$mailType = explode("-",$checkboxvalue);
									$sendmail_id = $mailType[0];
									$hid=$mailType[1];
									$status=0;
									$date=date("Y-m-d G:i:s");
									$sql5="select * from users_newsletters where email_id='".$sendmail_id."' and news_id='".$_POST['title']."' and status='0'";
									$ress=mysql_query($sql5,$con);
									$result=mysql_num_rows($ress);
									if($result=='0'){
										$sql4=mysql_query("INSERT INTO users_newsletters(email_id,news_id,added_date,status)
										VALUES('".$sendmail_id."','".$_POST['title']."','".$date."','".$status."')");
										$resultSet=$sql4;
									}
								}
								if($resultSet==''){?>
								<script>alert('Already Inserted');</script>
							<?}
							if($resultSet>0){?>
								<script>alert('Sent successfully.');</script>
							<?}
						}
						?>
			</div>
			<div style="text-align:center;">
                 <a  class="news_btn"  href="#" id="sendEmail"  onclick="return sendMailCheck();">Save</a>
			</div>
        </form>
		</div>
		<a href="unsubscribe.php?email=rajgorantla@gmail.com">UnSubScribe</a>
    </body>
</html>
<?php include('footer.php')?>
<script>
$(document).ready(function () {
     $('#alertLoader').html('<img src="images/spiffygif.gif"/>');
			$.ajax({
				type:'GET',
				dataType:'json',
				url:'ajaxtable.php',
				success: function(data){
				$('#alertLoader').html('');
					$table=$('#example').DataTable( {
							data: data,
							//paging: false,
							columns: [
								{ data: 'action' },
								{ data: 'Id' },
								{ data: 'email' },
								{ data: 'Added_date' },
								{ data: 'Type'},
								{ data: 'status' }
							]
					});
				}
			});
});
	function checkAll(){
		if (document.getElementById('selectAll').checked) {
			for(var a=0; a<document.getElementsByTagName('input').length;a++){
			  document.getElementsByTagName('input')[a].checked=true;
			}
		}
		else {
			for( var j=0; j<document.getElementsByTagName('input').length;j++){
			   document.getElementsByTagName('input')[j].checked=false;
			}
		}
	}
	function checkVal(){ 
		var checkval=document.getElementsByName('checkbox[]');         
		var len; var count=0;
		len=checkval.length;
		for(var i=0;i<len;i++){ 
			if(checkval[i].checked){
				count++;
			}
		}
		if(len==count){
			document.getElementById('selectAll').checked=true;  
		}
		else{
			document.getElementById('selectAll').checked=false;
		}
	}
	function usersList(){
	 $('#alertLoader').html('<img src="images/spiffygif.gif"/>');
	  var flag=true;
	  var letters = /^[A-Za-z]+$/;
	  var app=$("#app").val();
		if(flag==false){
		 return false;
		}else{
			$.ajax({
				type:'POST',
				dataType:'json',
				url:'ajaxtable.php',
				data:{app:app},
				success: function(data){
					$('#alertLoader').html('');
					$table.destroy();
					$table=$('#example').DataTable( {
							data: data,
							columns: [
								{ data: 'action' },
								{ data: 'Id' },
								{ data: 'email' },
								{ data: 'Added_date' },
								{ data: 'Type'},
								{ data: 'status' }
							]
					});
				}
			});
		}
	}
	function sendMailCheck()
	{
		var flag=true;
		var letters = /^[A-Za-z]+$/;
		var title=$("#title").val();
		var app=$("#app").val();
		if(title==""){
		  alert('select title');
		  flag= false;
		}
		if($("input[name='checkbox[]']:checked").length<=0) {
			alert("check The  sendedMail checkbox");
			return false;
		}
		if(flag==false){
		 return false;
		}else{
		  $("#formid").submit();
		}
		
	}
</script>